#include <queue>
class Solution {
public:

    bool st[210][210];
    int dx[4] = {0, 0, 1, -1};
    int dy[4] = {1, -1, 0, 0};

    void bfs(int n, int m, vector<vector<char> >& grid, int x, int y)
    {
        queue<pair<int, int>> q;
        q.push({x, y});
        st[x][y] = true;
        while(q.size())
        {
            auto t = q.front(); q.pop();
            int cx = t.first, cy = t.second;
            for(int k = 0; k < 4; k++)
            {
                int nx = cx + dx[k], ny = cy + dy[k];
                if(nx >= 0 && ny >= 0 && nx < n && ny < m && grid[nx][ny] == '1' && !st[nx][ny])
                {
                    st[nx][ny] = true;
                    q.push({nx, ny});
                }
            }
        }
    }

    int solve(vector<vector<char> >& grid) 
    {
        int n = grid.size();
        int m = grid[0].size();
        memset(st, 0, sizeof(st));
        int cnt = 0;
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
            {
                if(grid[i][j] == '1' && !st[i][j])
                {
                    cnt++;
                    bfs(n, m, grid, i, j);
                }
            }
        }
        return cnt;
    }
};
